package cn.lili.util;

import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.utils.SpringContextUtil;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.order.cart.entity.dto.TradeDTO;
import cn.lili.modules.system.entity.dos.LiVip;
import com.api.UserApi;
import com.pojos.OpenAppUser;
import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

public class GoodsSkuPriceUtil {

    /**
     * 获取售卖价
     * @param goodsSku 商品
     * @return 价格
     */
    public static Double getPrice(GoodsSku goodsSku, TradeDTO tradeDTO) {
        System.out.println("goodsSku = " + getIsPrice(goodsSku, tradeDTO));
        // 判断是否套餐商品
        if ((null != goodsSku.getIsMeal() && goodsSku.getIsMeal()) &&
                (null != goodsSku.getIsDefaultMeal() && goodsSku.getIsDefaultMeal())) {
            // 判断套餐商品购买渠道，结算页购买，则取套餐价格
            if (null != tradeDTO.getMealGoodsSource() && 0 == tradeDTO.getMealGoodsSource()) {
                return goodsSku.getMealPrice();
            }
        }

        //是否飞语会员
        if(StringUtils.isNotEmpty(tradeDTO.getSessionId())){
            UserApi userApi = SpringContextUtil.getBean(UserApi.class);
            OpenAppUser openAppUser = userApi.getUserinfo(tradeDTO.getSessionId());
            System.out.println("getPrice -- sessionId end ==" + openAppUser);
            System.out.println("getPrice -- sessionId end ==1" + goodsSku.getFyVipPrice());
            if(openAppUser!=null && "1".equals(openAppUser.getIs_vip()) && goodsSku.getFyVipPrice() !=null && goodsSku.getFyVipPrice() != 0){
                System.out.println("getPrice -- sessionId end ==2" + goodsSku.getFyVipPrice());
                System.out.println("getPrice -- sessionId end ==3" + goodsSku.getFyVipPrice());
                return goodsSku.getFyVipPrice();
            }
        }

        return getIsPrice(goodsSku, tradeDTO) ? goodsSku.getVipPrice() : goodsSku.getPrice();
    }

    /**
     * 是否会员价
     *
     * @param goodsSku 商品
     * @param tradeDTO
     * @return 是否
     */
    public static Boolean getIsPrice(GoodsSku goodsSku, TradeDTO tradeDTO) {
        return tradeDTO.getIsVip() != null && tradeDTO.getIsVip() && goodsSku.getVipPrice() != null &&
                goodsSku.getVipPrice() > 0 && goodsSku.getVipPrice() < goodsSku.getPrice();
    }

    public static Boolean getIsVip(LiVip liVip) {
        return liVip != null && liVip.getEndTimeTow() != null && liVip.getEndTimeTow().getTime() > new Date().getTime() && Integer.valueOf(1).equals(liVip.getVipStatus());
    }
}
